#include <gui/GuiButton.h>

GuiButton::GuiButton(Device * device, GuiLabel * label, std::string textureFile, std::string clickSoundFile, std::string hoverSoundFile, float x, float y, float width, float height, long borderSize) {
   D3DXIMAGE_INFO* info = new D3DXIMAGE_INFO;
   _image = LoadResourceFromFile(RM_TEXTURE, textureFile, device);

   D3DXGetImageInfoFromFile(textureFile.c_str(), info);
   _texwidth = (float) info->Width;
   _texheight = (float) info->Height;
   
   _clickSound = -1;
   _hoverSound = -1;

   if (clickSoundFile != "")
      _clickSound = LoadResourceFromFile(RM_SOUND, clickSoundFile);

   if (hoverSoundFile != "")
      _hoverSound = LoadResourceFromFile(RM_SOUND, hoverSoundFile);

   _x = x;
   _y = y;
   _width = width;
   _height = height;
   _borderWidth = (float) borderSize;
   _caption = label;
   _caption->SetCoords(_x + _borderWidth, _y + _borderWidth);
   _caption->SetWidth(_width - _borderWidth);
}

GuiButton::GuiButton(GuiLabel * label, long textureFile, long clickSoundFile, long hoverSoundFile, float x, float y, float width, float height, long borderSize)  {
   // TODO: texwidth/height?
   _image = textureFile;
   _clickSound = clickSoundFile;
   _hoverSound = hoverSoundFile;

   _x = x;
   _y = y;
   _width = width;
   _height = height;
   _borderWidth = (float) borderSize;
   _caption = label;
   _caption->SetCoords(_x + _borderWidth, _y + _borderWidth);
   _caption->SetWidth(_width - _borderWidth);
}

GuiButton::~GuiButton()  {
   delete _caption;
}

void GuiButton::Update(long mouseX, long mouseY, unsigned long key, double tElapsed)  {
   
}

void GuiButton::Draw(Device * device, double tElapsed) const {
   LPDIRECT3DTEXTURE9 tex = ((Texture *) GetResource(_image))->getTexture();
   float wstart = (float) _borderWidth / _texwidth;
   float hstart = (float) _borderWidth / _texheight;
   float botstart = (float) _height - (_borderWidth + _borderWidth);
   float rightstart = (float) _width - (_borderWidth + _borderWidth);

   device->setTexture(tex);

   // middle
   device->drawRect(_x + _borderWidth, _y + _borderWidth, rightstart, botstart, 0.0f, wstart, hstart, (_texwidth - (_borderWidth + _borderWidth)) / _texwidth, (_texheight - (_borderWidth + _borderWidth)) / _texheight );

   // top border
   device->drawRect(_x, _y, _borderWidth, _borderWidth, 0.0f, 0.0f, 0.0f, wstart, hstart);
   device->drawRect(_x + _borderWidth, _y, rightstart, _borderWidth, 0.0f, wstart, 0.0f, (_texwidth - (_borderWidth + _borderWidth)) / _texwidth, hstart );
   device->drawRect(_x + (_width - _borderWidth), _y, _borderWidth, _borderWidth, 0.0f, (_texwidth - _borderWidth) / _texwidth, 0.0f, wstart, hstart );

   // middle borders
   device->drawRect(_x, _y + _borderWidth, _borderWidth, hstart, 0.0f, 0.0f, hstart, wstart, (_texheight - (_borderWidth + _borderWidth)) / _texheight );
   device->drawRect(_x + (_width - _borderWidth), _y + _borderWidth, _borderWidth, hstart, 0.0f, (_texwidth - _borderWidth) / _texwidth, hstart, wstart, (_texheight - (_borderWidth + _borderWidth)) / _texheight );

   // bottom border
   device->drawRect(_x, _y + (_height - _borderWidth), _borderWidth, _borderWidth, 0.0f, 0.0f, (_texheight - _borderWidth) / _texheight, wstart, hstart );
   device->drawRect(_x + _borderWidth, _y + (_height - _borderWidth), rightstart, _borderWidth, 0.0f, wstart, (_texheight - _borderWidth) / _texheight, (_texwidth - (_borderWidth + _borderWidth)) / _texwidth, hstart );
   device->drawRect(_x + (_width - _borderWidth), _y + (_height - _borderWidth), _borderWidth, _borderWidth, 0.0f, (_texwidth - _borderWidth) / _texwidth, (_texheight - _borderWidth) / _texheight, wstart, hstart );

   // caption
   _caption->Draw(device, tElapsed);
}

